home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / abbrev5 / abbrev.cb next >
Text File  |  1990-01-17  |  15KB  |  363 lines

  1. /*************************************************************************
  2. *                                                                                                *
  3. *     If you are a previous user of ABBREV (previous to ABBREV4),              *
  4. *     you will need to replace the autoload's that were provided                  *
  5. *     with that version    with the ones specified below.                             *
  6. *                                                                                                  *
  7. *     ABBREV5 allows abbrev to work in conjunction with Brief's                 *
  8. *     file name completion facility (i.e. if <Tab> is pressed                     *
  9. *     while an abbrevion is on the command line, the command                     *
  10. *     will be expanded by abbrev, and then handed over to Brief's                 *
  11. *     file completion facility).  For user's of ABBREV4, simply                 *
  12. *     compile this file and the new ablookup.cb, and the new facility         *
  13. *  will be in place.                                                                          *
  14. *                                                                                                  *
  15. *     For users of previous versions, follow the instructions                     *
  16. *     below.                                                                                     *
  17. *                                                                                                *
  18. **************************************************************************
  19. *                                                                                                *
  20. *      ABBREV.CB    (Version 5)                                                                 *
  21. *                                                                                                  *
  22. *      This macro provides a replacement for edit_file and read_file.  It     *
  23. *      allows arbitrary abbreviation of drive and path combinations in         *
  24. *      file names (and may even be used to abbreviate full file names).         *
  25. *                                                                                                  *
  26. *      The if there is a slash or backslash in the path (after column 1),     *
  27. *      then abbrev looks up a replacement for what preceeds the                 *
  28. *      1st slash (backslash) and passes it on to the next                         *
  29. *      program in the chain.     If there is no slash or backslash,                 *
  30. *      abbrev checks for a replacement of the entire name                         *
  31. *      specified.  (This is for using an abbreviation for a                         *
  32. *      file name.)  It is not necessary to use the last                             *
  33. *      directory name as the abbreviation, and you may                             *
  34. *      supply several abbreviations for a single directory.                         *
  35. *                                                                                                  *
  36. *      Finally, if you wish to pass a name through without being                 *
  37. *      altered by abbrev, then precede it with an exclaimation point.         *
  38. *                                                                                                  *
  39. *      If "foo" was the abbreviation for "c:\new\dos\foo"                         *
  40. *                                                                                                  *
  41. *      then (edit_file "foo\filename") would pass on:                                 *
  42. *                                                                                                  *
  43. *              (edit_file "c:\new\dos\foo\filename").                                 *
  44. *                                                                                                  *
  45. *      The abbreviations are kept in a file called "abbrev.dat".                 *
  46. *      This file goes in the last directory specified by your                     *
  47. *      BPATH environment variable.    If you would like it in a                     *
  48. *      different directory, then you may specify this with the                     *
  49. *      BABBREV environment variable.  The macro "abbrev" (invoked with         *
  50. *      F10) will bring the abbrev.dat file into a Brief buffer to be             *
  51. *      edited.                                                                                     *
  52. *                                                                                                  *
  53. *      Each abbreviation is specified on 1 line of the file.                     *
  54. *      The line is of the form:                                                             *
  55. *                                                                                                  *
  56. *      <abbreviation> <whitespace> <substitution>                                     *
  57. *                                                                                                  *
  58. *      The <abbreviation> must begin in column 1.    The line                         *
  59. *      for the above example would be:                                                     *
  60. *                                                                                                  *
  61. *      foo         c:\new\dos\foo                                                             *
  62. *                                                                                                  *
  63. *      To inspire you of its usefulness, part of my current file is             *
  64. *      setup as follows:                                                                     *
  65. *                                                                                                  *
  66. *      autoexec c:\autoexec.bat                                                             *
  67. *      exec      c:\autoexec.bat                                                             *
  68. *      config     c:\config.sys                                                                 *
  69. *      ap         d:\pinball\apple                                                             *
  70. *      apple     d:\pinball\apple                                                             *
  71. *      util      d:\util                                                                         *
  72. *      u          d:\util                                                                         *
  73. *      uni         d:\util\uni                                                                 *
  74. *      mine      c:\soft\brief\macros\mine                                                 *
  75. *      m          c:\soft\brief\macros\mine                                                 *
  76. *      macros     c:\soft\brief\macros                                                     *
  77. *      mac         c:\soft\brief\macros                                                     *
  78. *      macs      c:\soft\brief\macros                                                     *
  79. *      inc         c:\soft\c\include                                                         *
  80. *      include  c:\soft\c\include                                                         *
  81. *                                                                                                  *
  82. *      To install, add the following to your initials macro                         *
  83. *      (after any other replacements to edit_file or read_file):                 *
  84. *                                                                                                  *
  85. *   autoload ("abbrev.cm", "edit_file", "read_file", "abbrev")                  *
  86. *      autoload ("ablookup.cm", "ab_lookup", "get_abbrev_file")                 *
  87. *                                                                                                  *
  88. *      Place "abbrev.cm" and "ablookup.cm" in a BPATH directory                 *
  89. *      (e.g. c:\brief\macros).                                                             *
  90. *                                                                                                  *
  91. *      Abbrev keeps the abbreviation file in a system buffer.                     *
  92. *      The 1st call will create the system buffer.   Subsequent calls         *
  93. *      will run faster.                                                                         *
  94. *                                                                                                  *
  95. *      Larry DeMar     July, 1989                                                             *
  96. *                                                                                                  *
  97. *      Updated for FILE_ED3 compatability,  August, 1989.   -Led                 *
  98. *                                                                                                  *
  99. *      Updated to allow suffix.cb to use the utilities without loading         *
  100. *      replacement macros (ABBREV4) October, 1989  -Led.                               *
  101. *                                                                                                *
  102. **************************************************************************
  103. *                                                                                                 *
  104. *      Version 5 has the following improvements:                                     *
  105. *                                                                                                *
  106. *      Updated to work with Brief's File completion facility.                     *
  107. *                                                                                                 *
  108. *      Supresses command line jibberish generated by                                 *
  109. *         called primitives.                                                                 *
  110. *                                                                                                 *
  111. *      Automatically Invokes Brief's file completion if * or ?                     *
  112. *         is on command    line when <Enter> is pressed.  Please note             *
  113. *          that the time "Expanding Wildcard..." is displayed,                     *
  114. *          Brief is doing its file completion.  Abbrev does very                 *
  115. *          little during this delay (compare to the time required                 *
  116. *          without abbrev installed).                                                     *
  117. *                                                                                                  *
  118. *      January, 1990 -Led                                                                     *
  119. *                                                                                                *
  120. *************************************************************************/
  121. #define ABBREV_FILE_NAME "abbrev.dat"
  122. #define TRUE 1
  123. #define FALSE 0
  124.  
  125. extern ab_lookup(...);
  126. extern get_abbrev_file(...);
  127. extern _bad_key();
  128.  
  129. replacement edit_file (...)
  130. {
  131.     string e_parm;
  132.  
  133.     if (inq_called () != "")
  134.         return edit_file ();
  135.  
  136.     if (!get_parm (0, e_parm, "File: "))
  137.         return;
  138.     //
  139.     //        Before passing our value down...see if there are any wildcard
  140.     //        characters.
  141.     //
  142.  
  143.     if ((e_parm = wild(e_parm, "File: ")) == "")
  144.         return;
  145.  
  146.     edit_file (abbr(e_parm));
  147. }
  148.  
  149. replacement read_file (...)
  150. {
  151.     string r_parm;
  152.  
  153.     if (inq_called () != "")
  154.         return read_file ();
  155.     if (!get_parm (0, r_parm, "File to read: "))
  156.         return;
  157.  
  158.     if ((r_parm = wild(r_parm, "File to read: ")) == "")
  159.         return;
  160.  
  161.     read_file (abbr(r_parm));
  162. }
  163.  
  164. /*************************************************************************
  165. *                                                                                                *
  166. *     WILD                                                                                         *
  167. *                                                                                                  *
  168. *     This is called to check for wildcard characters in the                     *
  169. *     prompt and force file name completion if wildcard                             *
  170. *     characters are detected.                                                             *
  171. *                                                                                                  *
  172. *     It returns the final prompt result, or "" if the                             *
  173. *     user escapes.                                                                             *
  174. *                                                                                                *
  175. *************************************************************************/
  176. wild(string e_parm, string our_prompt)
  177. {
  178.     while(index(e_parm, "*") || index(e_parm, "?")){
  179.         //
  180.         //        Put this public service message up.
  181.         //        Brief takes tremendous amounts of time expanding 
  182.         //        Wildcards.
  183.         //
  184.         message ("Expanding wildcard...");
  185.         push_back (key_to_int ("<Tab>"));    // Put a tab out to tell Brief to expand
  186.         if (!get_parm (NULL, e_parm, our_prompt, NULL, e_parm))
  187.             return ("");
  188.     }
  189.     return(e_parm);
  190. }
  191.  
  192. /*************************************************************************
  193. *                                                                                                *
  194. *      ABBR                                                                                         *
  195. *                                                                                                  *
  196. *      This is called to expand an abbreviation if necessary.                     *
  197. *                                                                                                  *
  198. *      It returns the string that should be passed on.                             *
  199. *                                                                                                *
  200. *************************************************************************/
  201. abbr (...)
  202. {
  203.     int slash_index;
  204.     global int ab_buf;
  205.     string abbreviation,
  206.              file_name,
  207.              first_char;          // allow leading slash or backslash as a courtesy 
  208.  
  209.     get_parm (0, file_name);
  210.  
  211.     if (substr (file_name, 1, 1) == "!")     // check for bang                     
  212.         return substr (file_name, 2);          // strip it and we're done.         
  213.     //                                                                                              
  214.     //   As a courtesy, if the user preceeded his abbreviation                     
  215.     //   with slash or backslash (from using dos too much)                         
  216.     //   lookup what follows the leading seperator                                     
  217.     //                                                                                              
  218.     //   If you want to use that leading slash or backslash                         
  219.     //   to shut off abbrev, then comment out the next 2                             
  220.     //   Brief statements (next 6 lines).                                                 
  221.     //                                                                                              
  222.     first_char = substr (file_name, 1, 1);               // get 1st char          
  223.     if (first_char == "/" || first_char == "\\")
  224.         file_name = substr (file_name, 2);             // lookup rest                 
  225.     else
  226.         first_char = "";                                    // nothing stripped             
  227.     //                                                                                              
  228.     //   now we have to see if there is a path separator.                          
  229.     //                                                                                              
  230.     slash_index = get_first_seperator (file_name);
  231.  
  232.     //       slash_index is now zero if there are now separators                  
  233.     //       and has the position of the 1st seperator if non-zero.              
  234.     //                                                                                              
  235.     if (slash_index)
  236.         abbreviation = substr (file_name, 1, slash_index - 1);
  237.     else
  238.         abbreviation = file_name;
  239.  
  240.     if (index (abbreviation, "*") || index (abbreviation, "?"))     // for FILE_ED3 if it has a star         
  241.         abbreviation = first_char + abbreviation;      // don't look up!         
  242.  
  243.     else
  244.         if (!ab_lookup (ABBREV_FILE_NAME, abbreviation, ab_buf, "abbrev.$b$"))
  245.             abbreviation = first_char + abbreviation;      // not found..put 1st char back. 
  246.     //                                                                                              
  247.     //   now....if there was a path...we must concatonate whats beyond 1st seperator 
  248.     //                                                                                              
  249.     if (slash_index)
  250.         abbreviation = abbreviation + substr (file_name, slash_index);
  251.  
  252.     return abbreviation;
  253. }
  254.  
  255. /*************************************************************************
  256. *                                                                        *
  257. *      ABBREV                                                                                     *
  258. *                                                                                                  *
  259. *      This is called to bring the abbreviation file into the                     *
  260. *      editor.  It first deletes the system buffer such that                     *
  261. *      changes (that are written) will be used on the next                         *
  262. *      request.                                                                                 *
  263. *                                                                        *
  264. *************************************************************************/
  265. abbrev (...)
  266. {
  267.     if (ab_buf)
  268.         delete_buffer (ab_buf);
  269.     ab_buf = 0;
  270.     edit_file (get_abbrev_file (ABBREV_FILE_NAME));
  271. }
  272.  
  273. /*************************************************************************
  274. *                                                                        *
  275. *      GET_FIRST_SEPERATOR                                                                     *
  276. *                                                                                                  *
  277. *      This is called to return the position of the 1st seperator                 *
  278. *      (slash or backslash) in a file name.    It returns 0 if                     *
  279. *      no seperators exist, or the index of the 1st one it                         *
  280. *      finds.                                                                                     *
  281. *                                                                        *
  282. *************************************************************************/
  283. get_first_seperator (...)
  284. {
  285.     int slash_index,
  286.          bslash_index;
  287.  
  288.     string file_name;
  289.  
  290.     get_parm (0, file_name);
  291.  
  292.     slash_index = index (file_name, "/");
  293.     bslash_index = index (file_name, "\\");
  294.     if (slash_index && bslash_index)       // if both seperators are there     
  295.         if (bslash_index < slash_index)
  296.             slash_index = bslash_index;    // then take the lower                     
  297.         else ;
  298.     else
  299.         if (                        // is 1st zero?                                             
  300.             bslash_index)
  301.             slash_index = bslash_index;    // yep...use 2nd.                          
  302.     return slash_index;
  303. }
  304.  
  305. /*************************************************************************
  306. *                                                                                                *
  307. *     _BAD_KEY                                                                                     *
  308. *                                                                                                  *
  309. *     This replacement for bad key allows Abbrev and Brief's                     *
  310. *     file name completion to happily co-exist.                                         *
  311. *                                                                                                  *
  312. *     If the current command is edit_file or read_file, and                         *
  313. *     the bad key is <Tab>, then we see if there is a substitution             *
  314. *     to make on the command line.  If there is, we make the                     *
  315. *     substitution, and push a <Tab> back on the keyboard stack.                 *
  316. *                                                                                                  *
  317. *     The <Tab> is necessary because Brief's _bad_key file completion         *
  318. *     routine reads directly from the command line (as opposed to                 *
  319. *     passing the command string up and down the chain).  We then                 *
  320. *     ignore the 2nd <Tab> and let the file completion facility                 *
  321. *     do its thing.                                                                             *
  322. *                                                                                                *
  323. *************************************************************************/
  324. replacement _bad_key ()
  325. {
  326.     global int ab_flag;
  327.  
  328.     int    key_pressed = read_char ();
  329.     string    cmd_lin,
  330.                 command = inq_command ();
  331.     //
  332.     //     No matter what key it was we put it back.
  333.     //        If its a tab, it also goes back because we need to
  334.     //        do a file name completion.
  335.     //
  336.  
  337.     push_back (key_pressed);  
  338.  
  339.     if ((command == "edit_file" || command == "read_file") 
  340.             && (key_pressed == key_to_int ("<Tab>"))){
  341.         //
  342.         //        If our flag is set, we have already processed.
  343.         //        Clear the flag and let it through
  344.         //
  345.         if (ab_flag){
  346.             ab_flag = FALSE;
  347.             return(_bad_key());
  348.         }
  349.         cmd_lin = abbr(inq_cmd_line());        // abbreviate what's there
  350.         if (cmd_lin == inq_cmd_line())        // if it didn't change
  351.             return(_bad_key());                    //     we're done...pass it down.
  352.         //
  353.         //        We made a change.....we have to get it back to the command line
  354.         //
  355.         ab_flag = TRUE;                                    // Cause us to ignore this req.
  356.         return (cmd_lin);                                    // Pass back completed string.
  357.     }
  358.     //
  359.     //        Not of interest to us....pass the bad key down.
  360.     //
  361.     return(_bad_key());
  362. }
  363.